import Vue from 'vue'
import Vuex from 'vuex'
import { getItem, setItem } from '@/utils/storage.js'

Vue.use(Vuex)

const USER_KEY = 'toutiao-user'

export default new Vuex.Store({
  state: {
    // user: null // 当前登录用户的数据(登录状态)
    user: getItem(USER_KEY),
    cachePage: ['LayoutIndex'] // 默认缓存 LayoutIndex 页面
  },
  mutations: { // 修改state状态的唯一途经
    setUser (state, data) {
      state.user = data
      //  刷新数据就初始化了, 使用本地存储持久化( 仅仅是为了持久化 )
      setItem(USER_KEY, data)
    },
    // 添加缓存页面
    addCachePage (state, pageName) {
      if (!state.cachePage.includes(pageName)) {
        state.cachePage.push(pageName)
      }
    },
    // 移除缓存页面
    removeCachePage (state, pageName) {
      const index = state.cachePage.indexOf(pageName)
      if (index !== -1) {
        state.cachePage.splice(index, 1)
      }
    }
  },
  actions: {
  },
  modules: {
  }
})
